package com.xiu.travel;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@MapperScan({"com.xiu.travel.security.mapper"})
@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)
@EnableTransactionManagement
@Slf4j
@EnableAsync
@EnableFeignClients(basePackages = { "com.xiu.travel.portal.client"})
public class TravelApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(TravelApplication.class);


        ConfigurableApplicationContext application = app.run(args);
        Environment env = application.getEnvironment();
        try {
            log.info(
                    """

                            ----------------------------------------------------------
                            \tApplication '{}' is running! Access URLs:
                            \tLocal: \t\thttps://localhost:{}
                            \tExternal: \thttps://{}:{}
                            \tDoc: \thttps://localhost:{}/doc.html
                            ----------------------------------------------------------""",
                    env.getProperty("spring.application.name"),
                    env.getProperty("server.port"),
                    InetAddress.getLocalHost().getHostAddress(),
                    env.getProperty("server.port"),
                    env.getProperty("server.port"));

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

//    /**
//     * http自动跳转https
//     */
//    @Bean
//    @ConditionalOnProperty(name = {"server.port.ssl.enabled"}, havingValue = "true")
//    public ServletWebServerFactory servletContainer() {
//        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
//            @Override
//            protected void postProcessContext(Context context) {
//                SecurityConstraint securityConstraint = new SecurityConstraint();
//                securityConstraint.setUserConstraint("CONFIDENTIAL");
//                SecurityCollection collection = new SecurityCollection();
//                collection.addPattern("/*");
//                securityConstraint.addCollection(collection);
//                context.addConstraint(securityConstraint);
//            }
//        };
//        tomcat.addAdditionalTomcatConnectors(redirectConnector());
//        return tomcat;
//    }
//
//    private Connector redirectConnector() {
//        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//        connector.setScheme("http");
//        connector.setPort(8080); // 原http端口
//        connector.setSecure(false);
//        connector.setRedirectPort(443); // 跳转的https端口，也就是我们配置文件中配置的项目端口
//        return connector;
//    }

}
